package com.intel.factorytest.ChamberMode;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Looper;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.app.ActionBar;
import android.os.SystemProperties;

import com.intel.factorytest.R;
import com.intel.factorytest.util.LogUtils;
import com.intel.factorytest.util.Util;
import com.intel.factorytest.util.ShellUtilsPlus;
import com.intel.factorytest.ChamberMode.ChamberModeMainActivity;



public class WifiTest extends ChambermodeBaseActivity {
    
    private final static int WIFI_TEST_MSG_UPDATE_UI = 1;
    private final static int WIFI_TEST_MSG_TIME_OUT = 2;
    private final static int WIFI_TEST_MSG_FTM_IN = 3;    
    private final static int WIFI_TEST_MSG_FTM_OUT = 4;
    private final static int WIFI_TEST_MSG_RMMOD_WLAN = 5;
    private final static int WIFI_TEST_MSG_INSMOD_WLAN = 6;
    private final static int WIFI_TEST_MSG_WLAN_UP = 7;
    private final static int WIFI_TEST_MSG_WLAN_CON_MODE = 8;
    private final static int WIFI_TEST_MSG_WLAN_FTMDAEMON = 9;
    private final static int WIFI_TEST_MSG_2_4G_TRANSMIT = 10;
    private final static int WIFI_TEST_MSG_5G_TRANSMIT = 11;
    private final static int WIFI_TEST_MSG_STOP_TRANSMIT = 12;

    private static String WIFI_TRAN_FTM_IN_DIAG_CMD_FILE = "/system/bin/diagcmd/bandtestin.txt";    
    private static String WIFI_TRAN_FTM_OUT_DIAG_CMD_FILE = "/system/bin/diagcmd/bandtestout.txt";  
    //private static String WIFI_2_4G_TRAN_CMD = "myftm -M 0 -r 14 -f 2412 -c 0 -p 20 -a 1 -t 3";
    private static String WIFI_2_4G_TRAN_CMD = "myftm -M 4 -r 1 -f 2412 -c 0 -p 18 -a 1 -t 3";
    //private static String WIFI_5G_TRAN_CMD = "myftm -a 3 -M 1 -r 22 -f 5180 -c 0 -p 20 -t 3";
    private static String WIFI_5G_TRAN_CMD = "myftm -a 1 -M 0 -c 0 -r 4 -f 5180 -p 15 -t 3";
    private static String WIFI_STOP_TRAN_CMD = "myftm -t 0";
    private static String WIFI_RMMOD_WLAN_CMD = "rmmod wlan";
    private static String WIFI_INSMOD_WLAN_CMD = "insmod /vendor/lib/modules/qca_cld3_wlan.ko";
    private static String WIFI_IFCONFIG_WLAN_CMD = "ifconfig wlan0 up";
    private static String WIFI_FTMDAEMON_CMD = "ftmdaemon -n -dd";

    /*
    private int mTimeout = 20000;    
    private WifiTestDown mWifiTestDown = null;
    */
    ShellUtilsPlus.CommandResult mCmdResult;
    private String cmdSuccessMsg;
    private String cmdErrorMsg;
    private boolean isWifi1Transmiting = false;
    private boolean isWifiTransmiting = false;

    
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            LogUtils.loge("mHandler msg.what = " + msg.what);
            switch (msg.what) {                
                case WIFI_TEST_MSG_UPDATE_UI: {                    
                    removeMessages(WIFI_TEST_MSG_UPDATE_UI);
                    updateTitle("");
                    break;
                }
                case WIFI_TEST_MSG_TIME_OUT: {     
                    removeMessages(WIFI_TEST_MSG_TIME_OUT);
                    //setWlanConMode("0");                    
                    SystemProperties.set("persist.sys.chamber.diagcmd","null");
                    Util.isWifiTesting = false ;
                    SystemProperties.set("persist.sys.chamber_run","true");
                    finish();
                    break;
                }
                case WIFI_TEST_MSG_FTM_IN: {
                    removeMessages(WIFI_TEST_MSG_FTM_IN);
                    enterFtmMode();                    
                    sendDelayedMsg(WIFI_TEST_MSG_2_4G_TRANSMIT,1000,null,null,mHandler);
                    break;
                }
                case WIFI_TEST_MSG_FTM_OUT: {
                    removeMessages(WIFI_TEST_MSG_FTM_OUT);
                    exitFtmMode();                    
                    sendDelayedMsg(WIFI_TEST_MSG_TIME_OUT,1000,null,null,mHandler);
                    break;
                }
                case WIFI_TEST_MSG_RMMOD_WLAN: {
                    removeMessages(WIFI_TEST_MSG_RMMOD_WLAN);
                    rmmodWlan();                    
                    sendDelayedMsg(WIFI_TEST_MSG_INSMOD_WLAN,2000,null,null,mHandler);
                    break;
                }
                case WIFI_TEST_MSG_INSMOD_WLAN: {
                    removeMessages(WIFI_TEST_MSG_INSMOD_WLAN);
                    insmodWlan();                    
                    sendDelayedMsg(WIFI_TEST_MSG_WLAN_UP,2000,null,null,mHandler);
                    break;
                }
                case WIFI_TEST_MSG_WLAN_UP: {
                    removeMessages(WIFI_TEST_MSG_WLAN_UP);
                    ifconfigWlan();                    
                    sendDelayedMsg(WIFI_TEST_MSG_WLAN_CON_MODE,2000,null,null,mHandler);
                    break;
                }
                case WIFI_TEST_MSG_WLAN_CON_MODE: {
                    removeMessages(WIFI_TEST_MSG_WLAN_CON_MODE);
                    setWlanConMode();
                    sendDelayedMsg(WIFI_TEST_MSG_2_4G_TRANSMIT,2000,null,null,mHandler);
                    break;
                }
                case WIFI_TEST_MSG_WLAN_FTMDAEMON: {
                    removeMessages(WIFI_TEST_MSG_WLAN_FTMDAEMON);
                    setFtmdaemon();
                    sendDelayedMsg(WIFI_TEST_MSG_2_4G_TRANSMIT,1000,null,null,mHandler);
                    break;
                }
                case WIFI_TEST_MSG_2_4G_TRANSMIT: {
                    removeMessages(WIFI_TEST_MSG_2_4G_TRANSMIT);                    
                    isWifi1Transmiting = true;
                    updateTitle("WIFI");
                    Wifi1Transmit();
                    isWifiTransmiting = true;
                    sendDelayedMsg(WIFI_TEST_MSG_UPDATE_UI,5000,null,null,mHandler);
                    sendDelayedMsg(WIFI_TEST_MSG_STOP_TRANSMIT,20000,null,null,mHandler);
                    break;
                }
                case WIFI_TEST_MSG_5G_TRANSMIT: {
                    removeMessages(WIFI_TEST_MSG_5G_TRANSMIT);                    
                    isWifi1Transmiting = false;
                    updateTitle("WIFI");                    
                    Wifi2Transmit();                    
                    isWifiTransmiting = true;
                    sendDelayedMsg(WIFI_TEST_MSG_UPDATE_UI,5000,null,null,mHandler);                    
                    sendDelayedMsg(WIFI_TEST_MSG_STOP_TRANSMIT,20000,null,null,mHandler);
                    break;
                }
                case WIFI_TEST_MSG_STOP_TRANSMIT: {
                    removeMessages(WIFI_TEST_MSG_STOP_TRANSMIT);
                    WifiStopTransmit();
                    if(isWifi1Transmiting) {
                       sendDelayedMsg(WIFI_TEST_MSG_5G_TRANSMIT,2000,null,null,mHandler);
                    }else{
                       sendDelayedMsg(WIFI_TEST_MSG_TIME_OUT,2000,null,null,mHandler);
                    }                    
                    isWifiTransmiting = false;
                    break;
                }
                default:
                    break;
            }
        }
    };

    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);
        LogUtils.loge("onCreate");
    }

    @Override
    public void onResume() {
        LogUtils.loge("onResume");        
        //SystemProperties.set("persist.sys.chamber.diagcmd","null");
        SystemProperties.set("persist.sys.chamber_run","false");
        super.onResume();
    }

    @Override
    protected void startTest(){
        LogUtils.logi("startWIFITest");
        Util.isWifiTesting = true ;
        updateMessage("WIFI TEST");
        //sendDelayedMsg(WIFI_TEST_MSG_RMMOD_WLAN,0,null,null,mHandler);WIFI_TEST_MSG_WLAN_CON_MODE
        sendDelayedMsg(WIFI_TEST_MSG_WLAN_CON_MODE,0,null,null,mHandler);
    }
    
    @Override
    protected void stopTest(){
        LogUtils.logi("stopWIFITest");
        WifiStopTransmit();
        sendDelayedMsg(WIFI_TEST_MSG_TIME_OUT,1000,null,null,mHandler);
    }

    @Override
    public void onPause() {
        super.onPause();
        LogUtils.loge( "onPause() ");
    }
    
    @Override
    public void onStop() {
        super.onPause();
        LogUtils.loge( "onStop() ");        
        WifiStopTransmit();
        //exitFtmMode();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.loge( "onDestroy() ");
    }
    
    @Override
    public void finish() {
        Intent i = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString(ChamberModeMainActivity.WIFI_RESULT, "wifi_test_finish");        
        i.putExtras(bundle);
        setResult(ChamberModeMainActivity.START_WIFI_TEST_RESULT_CODE,i);
        super.finish();
    }

    private Thread mWifiTestFtmThread;

    public void ftmExecThread(String path) {        
        LogUtils.logi( "ftmExecThread path = " + path);
        mWifiTestFtmThread = new Thread(new WifiTestFtmRunnable(path));
        mWifiTestFtmThread.start();
    }

    private void enterFtmMode(){
        LogUtils.logi("enterFtmMode");
        updateMessage("FTM Mode");
        ftmExecThread(WIFI_TRAN_FTM_IN_DIAG_CMD_FILE);
    }
    
    private void exitFtmMode(){
        LogUtils.logi("exitFtmMode");
        ftmExecThread(WIFI_TRAN_FTM_OUT_DIAG_CMD_FILE);
    }

    private class WifiTestFtmRunnable implements Runnable{

        String bandPath;
        public WifiTestFtmRunnable(String bandPath){
            this.bandPath = bandPath;
        }

        @Override
        public void run() {
            LogUtils.logi("WifiTestFtmRunnable run() bandpath = " + bandPath);
            mCmdResult = ShellUtilsPlus.execCommand("diag_tools -f" + bandPath,false);
            cmdSuccessMsg = mCmdResult.successMsg;
            cmdErrorMsg = mCmdResult.errorMsg;
            LogUtils.logi( "WifiTestFtmRunnable get Success Msg = " + cmdSuccessMsg + ", Error Msg = " + cmdErrorMsg);
        }
    }

    private Thread mWifiTestMyFtmCmdThread;

    public void MyFtmCmdExtcThread(String cmdpath) {        
        LogUtils.logi( "MyFtmCmdExtcThread cmdpath = " + cmdpath);
        mWifiTestMyFtmCmdThread = new Thread(new WifiTestMyFtmCmdRunnable(cmdpath));
        mWifiTestMyFtmCmdThread.start();
    }

    private void Wifi1Transmit(){
        LogUtils.logi("Wifi1Transmit");
        updateMessage("wifi 2.4G");        
        SystemProperties.set("persist.sys.chamber.diagcmd","wlan1txstart");
        //MyFtmCmdExtcThread(WIFI_2_4G_TRAN_CMD);
    }

    private void Wifi2Transmit(){
        LogUtils.logi("Wifi2Transmit");
        updateMessage("wifi 5G");        
        SystemProperties.set("persist.sys.chamber.diagcmd","wlan2txstart");
        //MyFtmCmdExtcThread(WIFI_5G_TRAN_CMD); 
    }

    private void WifiStopTransmit(){
        LogUtils.logi("WifiStopTransmit");
        if(isWifiTransmiting) {
            updateMessage("wifi Transmit stop");
            //MyFtmCmdExtcThread(WIFI_STOP_TRAN_CMD); 
            SystemProperties.set("persist.sys.chamber.diagcmd","wlantxstop");
        }
    }

    private void rmmodWlan(){
        LogUtils.logi("rmmodWlan");
        updateMessage("rmmod Wlan");
        MyFtmCmdExtcThread(WIFI_RMMOD_WLAN_CMD); 
    }

    private void insmodWlan(){
        LogUtils.logi("insmodWlan");
        updateMessage("insmod Wlan");
        MyFtmCmdExtcThread(WIFI_INSMOD_WLAN_CMD); 
    }
    
    private void ifconfigWlan(){
        LogUtils.logi("ifconfigWlan");
        updateMessage("ifconfig wlan0 up");        
        SystemProperties.set("persist.sys.chamber.diagcmd","wlanup");
        //MyFtmCmdExtcThread(WIFI_IFCONFIG_WLAN_CMD); 
    }
    
    private void setWlanConMode(){
        
        SystemProperties.set("persist.sys.chamber.diagcmd","wlanmode");
        /*
        LogUtils.logi("setWlanConMode value = " + value);
        updateMessage("set Wlan con_mode");
        int con_mode1 = SystemProperties.getInt("persist.sys.conmode",100);
        LogUtils.loge("setWlanConMode con_mode1 = " + con_mode1);
        SystemProperties.set("persist.sys.conmode", value);
        int con_mode2 = SystemProperties.getInt("persist.sys.conmode",200);
        LogUtils.loge("setWlanConMode con_mode2 = " + con_mode2);
        */
    }
    
    private void setFtmdaemon (){
        LogUtils.logi("setFtmdaemon");
        updateMessage("ftmdaemon -n -dd");
        MyFtmCmdExtcThread(WIFI_FTMDAEMON_CMD); 
    }

    private class WifiTestMyFtmCmdRunnable implements Runnable{

        String myFtmCmdPath;
        public WifiTestMyFtmCmdRunnable(String myFtmCmdPath){
            this.myFtmCmdPath = myFtmCmdPath;
        }

        @Override
        public void run() {
            LogUtils.logi("WifiTestMyFtmCmdRunnable run() myFtmCmdPath = " + myFtmCmdPath);
            mCmdResult = ShellUtilsPlus.execCommand(myFtmCmdPath,false);
            cmdSuccessMsg = mCmdResult.successMsg;
            cmdErrorMsg = mCmdResult.errorMsg;
            LogUtils.logi( "WifiTestMyFtmCmdRunnable get Success Msg = " + cmdSuccessMsg + ", Error Msg = " + cmdErrorMsg);
        }
    }

    /*
    private class countDownThread extends Thread{
        public countDownThread(){}

        @Override
        public void run() {
            LogUtils.logi("countDownThread  run()");
            // TODO Auto-generated method stub
            Looper.prepare();
            mWifiTestDown = new WifiTestDown();
            mWifiTestDown.start();
            Looper.loop();
        }
    }

    private class WifiTestDown extends CountDownTimer{

        public WifiTestDown() {
            super(mTimeout,mTimeout);
        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            LogUtils.logi("Time Out:");
            mHandler.sendEmptyMessage(WIFI_TEST_MSG_TIME_OUT);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub
        }
    }
    */
}

